#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int kInf = 1e9;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  string s;
  vector<int> pos[26];

  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    pos[s[i] - 'a'].push_back(i + 1);
  }

  int ans = 0;
  for (const vector<int>& a : pos) {
    for (const vector<int>& b : pos) {
      if (a.empty() || b.empty() || a[0] == b[0]) {
        continue;
      }
      vector<int> sum(a.size() + b.size() + 1);
      int tot = 1;

      for (int i = 0, j = 0; i < a.size() || j < b.size();) {
        if (i >= a.size() || (j < b.size() && b[j] < a[i])) {
          sum[tot++] = -1;
          j++;
        } else {
          sum[tot++] = 1;
          i++;
        }
      }
      int minsum = kInf;
      int lastb = 0;
      for (int i = 1; i < sum.size(); i++) {
        if (sum[i] == -1) {
          for (; lastb < i; lastb++) {
            minsum = min(sum[lastb], minsum);
          }
        }
        sum[i] += sum[i - 1];
        ans = max(ans, sum[i] - minsum);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}